algalcommand.io.xml
Class Element

java.lang.Object
  extended by algalcommand.io.xml.Element
All Implemented Interfaces:
java.io.Serializable, java.lang.Cloneable
Direct Known Subclasses:
RootElement

public class Element
extends java.lang.Object
implements java.io.Serializable, java.lang.Cloneable

The SAX XML parser packaged with Java is not thread safe and fails miserably in the true multithreaded application such as this. As such, this class is part of a bare-bones XML writing and reading parser that is fully thread safe.

This class IS thread-safe!

Copyright Owner: Michigan State University, license number TEC2011-002101Prov

See Also:
Serialized Form

Constructor Summary
Element()
          default contsructor, try not to use it if you don't have to
Element(java.lang.String tagname)
          Constructs an Element object with the specified tagname
 
Method Summary
 void addAttribute(java.lang.String name, java.lang.String value)
          Deprecated. Use setAttribute(name, value) instead;
 void addChild(Element child)
          Adds an element child inside this tag.

Ex:
     <font size="2">Greetings <b>friend!</b></font>
In the above code, the children of the font element are (in order) "Greetings " and b.

 void addChild(java.lang.String textChild)
          Adds a portion of text as a child to this element.

Ex:
     <font size="2">Greetings <b>friend!</b></font>
In the above code, the children of the font element are (in order) "Greetings " and b.

 Element clone()
           
 boolean containsChild(java.lang.Object obj)
          Checks to see if the specified object is a child of this element
 int countChildren()
          Ex:
     <font size="2">Greetings <b>friend!</b></font>
In the above code, the font element has 2 children: "Greetings " and b.
 java.util.List<java.lang.String> getAllAttributeNames()
          Returns a list of the names of all of the attributes.
 java.lang.String getAttributeValue(java.lang.String name)
          Retrieves the value of an attribute
 java.lang.Object getChild(int index)
          Fetches a child from this element.
 java.util.List<Element> getChildElementsByName(java.lang.String tagname, boolean caseSensitive)
          Scans through the children of this element and returns those of the type specified.
 java.lang.String getTagName()
           
 boolean hasAttribute(java.lang.String name)
          Checks for th eexistance of an attribute
 boolean isEmpty()
          Determines if this element is empty.
static void main(java.lang.String[] args)
          Deprecated.  
 void removeAllAttributes()
          Removes all of the attributes from this element
 void removeAllChildren()
          Removes all of ht echildren from this element
 void removeAttribute(java.lang.String name)
          Removes the specified attribute from this element.
 void removeChild(int index)
          Removes the specified child from this element.
 void removeChild(java.lang.Object child)
          Removes the specified child from this element.
 void setAttribute(java.lang.String name, java.lang.String Value)
          Sets the value of an attribute for this element If the attribute does not already exist, it is automatically created.
 void setTagName(java.lang.String tagname)
          Sets the name of this tag.
 java.lang.String toString()
          This will convert this element into its XML representation, returned as a String.
 java.lang.String toString(int indentNumber)
          Converts this element and all of its children into their XML representation, with the indecated amount of extra indentation.
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Element

public Element()
default contsructor, try not to use it if you don't have to


Element

public Element(java.lang.String tagname)
Constructs an Element object with the specified tagname

Parameters:
tagname - The name of the tag (e.g. <font face="serif"> has a tagname of "font").
Throws:
java.lang.IllegalArgumentException - Thrown is tagname is not a valid name for an XML tag (has innappropriate characters or whitespace).
Method Detail

addAttribute

public void addAttribute(java.lang.String name,
                         java.lang.String value)
Deprecated. Use setAttribute(name, value) instead;

Adds an attribute to this element.

Parameters:
name - Name of the attribute
value - Value of the attribute

getAttributeValue

public java.lang.String getAttributeValue(java.lang.String name)
Retrieves the value of an attribute

Parameters:
name - The name of the attribute
Returns:
The value of the attribute, or null if the named attribute does not exist for this element.

setAttribute

public void setAttribute(java.lang.String name,
                         java.lang.String Value)
Sets the value of an attribute for this element If the attribute does not already exist, it is automatically created.

Parameters:
name - The name of the attribute
Value - The (new) value of the attribute
Throws:
java.lang.IllegalArgumentException - Thrown if name is not a valid name for an XML attribute (has innappropriate characters or whitespace).

hasAttribute

public boolean hasAttribute(java.lang.String name)
Checks for th eexistance of an attribute

Parameters:
name - Name of the attribute
Returns:
true if that attribute has been set for this element, false otherwise.

removeAttribute

public void removeAttribute(java.lang.String name)
Removes the specified attribute from this element.

Parameters:
name - The name of the attribute to remove

removeAllAttributes

public void removeAllAttributes()
Removes all of the attributes from this element


getAllAttributeNames

public java.util.List<java.lang.String> getAllAttributeNames()
Returns a list of the names of all of the attributes.

Returns:
A copy of the names of all of the attributes of this element, packed into a List object for convenience.

addChild

public void addChild(java.lang.String textChild)
Adds a portion of text as a child to this element.

Ex:
     <font size="2">Greetings <b>friend!</b></font>
In the above code, the children of the font element are (in order) "Greetings " and b. "Greetings " is a text child and b is an element child. b has "friend!" as its child ("friend!" is not a child of font).

Parameters:
textChild - A string of text to add

addChild

public void addChild(Element child)
Adds an element child inside this tag.

Ex:
     <font size="2">Greetings <b>friend!</b></font>
In the above code, the children of the font element are (in order) "Greetings " and b. "Greetings " is a text child and b is an element child. b has "friend!" as its child ("friend!" is not a child of font).

Parameters:
child - An element to add as a child to this element

countChildren

public int countChildren()
Ex:
     <font size="2">Greetings <b>friend!</b></font>
In the above code, the font element has 2 children: "Greetings " and b. "Greetings " is a text child and b is an element child. b has one child: "friend!". "friend!" is not a child of font.

Returns:
The number of children that this element has

getChild

public java.lang.Object getChild(int index)
Fetches a child from this element.

Parameters:
index - Index of the child (first child is index 0).
Returns:
The child, which should either be a String or another element object.

getChildElementsByName

public java.util.List<Element> getChildElementsByName(java.lang.String tagname,
                                                      boolean caseSensitive)
Scans through the children of this element and returns those of the type specified.

Parameters:
tagname - Tag name of the elements you want. This is not case sensitive.
Returns:
A list of Elements whose tag names match

removeChild

public void removeChild(int index)
Removes the specified child from this element.

Parameters:
index -

removeChild

public void removeChild(java.lang.Object child)
Removes the specified child from this element.

Parameters:
child - The object to be removed

removeAllChildren

public void removeAllChildren()
Removes all of ht echildren from this element


containsChild

public boolean containsChild(java.lang.Object obj)
Checks to see if the specified object is a child of this element

Parameters:
obj - An object that may or may not already be a child of this element.
Returns:
true if this element has obj as one of its children.

isEmpty

public boolean isEmpty()
Determines if this element is empty. An XML element is said to be empty if it has no children (nothing to put between opening and closing tags).

Returns:

getTagName

public java.lang.String getTagName()
Returns:
The name of this tag.

setTagName

public void setTagName(java.lang.String tagname)
Sets the name of this tag.

Parameters:
tagname - The new name for this tag.

toString

public java.lang.String toString()
This will convert this element into its XML representation, returned as a String.

Overrides:
toString in class java.lang.Object
Returns:
The XML representation of this element and all of its children. This is equivalent to invoking toString(0).

toString

public java.lang.String toString(int indentNumber)
Converts this element and all of its children into their XML representation, with the indecated amount of extra indentation.

Parameters:
indentNumber - How many extra tabs to indent
Returns:
The XML representation of this element and all of its children.

clone

public Element clone()
Overrides:
clone in class java.lang.Object
Returns:
Returns a deep copy of this object

main

public static void main(java.lang.String[] args)
Deprecated. 

testing only